package com.giibpt;

import java.util.List;

import com.giibpt.rifidi.EtiquetaActiva;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;

public class RastreoWindow extends Panel{
	long sleep = 1000;
	
	List<EtiquetaActiva> activos;
	
	Thread syncronizer;
	
	
	Table tblArea1;
	Table tblArea2;
	Table tblArea3;
	Table tblAlertas;
	Embedded embMap;
	
	public RastreoWindow() {
		this.activos = GiibptApplication.getActivos();
		
		tblArea1 =createTable();
		tblArea2 =createTable();
		tblArea3 =createTable();
		tblAlertas =createTable();
		
		tblAlertas.setHeight("100%");
		
		embMap = new Embedded("", new ThemeResource("images/map.jpg"));
		embMap.setWidth("100%");
//		embMap.setHeight("400px");
		refresh();
		GridLayout lytMain = new GridLayout(3,2);
		//VerticalLayout lytMain = new VerticalLayout();
		lytMain.setWidth("100%");
		 	
		lytMain.addComponent(tblArea1,  0, 0);
		lytMain.addComponent(tblArea2,  1, 0);
		lytMain.addComponent(tblArea3,  2, 0);
		lytMain.addComponent(tblAlertas, 0, 1 );
		lytMain.addComponent(embMap, 1, 1, 2, 1 );
		lytMain.setColumnExpandRatio(0, 1);
		lytMain.setColumnExpandRatio(1, 1);
		lytMain.setColumnExpandRatio(2, 1);
		lytMain.setComponentAlignment(embMap, Alignment.TOP_CENTER);
		
		lytMain.setSpacing(true);
		lytMain.setMargin(true);
		setContent(lytMain);
	}
	
	private Table createTable(){
		Table table = new Table();
		table.setContainerDataSource(createContainer());
		table.addGeneratedColumn("subarea", new ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				EtiquetaActiva e = (EtiquetaActiva) itemId;
				return new Label(e.getSubArea().getNombre());
			}
		});
		
		table.setVisibleColumns(new Object[]{"etiqueta", "lector", "subarea"});
		table.setColumnHeaders(new String[]{"Persona", "lector", "subarea"});
		table.setPageLength(4);
		
		table.setWidth("100%");
		return table;
	}
	private BeanItemContainer<EtiquetaActiva> createContainer(){
		BeanItemContainer<EtiquetaActiva> container = new BeanItemContainer<EtiquetaActiva>(EtiquetaActiva.class);
	
	//	container.addNestedContainerProperty("lector.idLector");
		return container;
	}
	
	@Override
	public void attach() {
		// TODO Auto-generated method stub
		super.attach();
		
		syncronizer = new Thread(){
			
			public void run() {
				try{
					while(true){
						refresh();
						sleep(sleep);
					}
				}catch (Exception e) {
				//	e.printStackTrace();
				}
				
			}
		};
		syncronizer.start();
	}
	
	
	@Override
	public void detach() {
		super.detach();
		syncronizer.interrupt();
	}
	@SuppressWarnings("unchecked")
	private void refresh(){
		List<EtiquetaActiva> activos = GiibptApplication.getActivos();
		
		tblArea1.getContainerDataSource().removeAllItems();
		tblArea2.getContainerDataSource().removeAllItems();
		tblArea3.getContainerDataSource().removeAllItems();
		tblAlertas.getContainerDataSource().removeAllItems();
		
		for(EtiquetaActiva activa : activos){
			switch(activa.getLector().getSubArea().getArea().getIdArea().intValue()){
				case 1:
					tblArea1.getContainerDataSource().addItem(activa);
					break;
				case 2:
					tblArea2.getContainerDataSource().addItem(activa);
					break;
				case 3:
					tblArea3.getContainerDataSource().addItem(activa);
					break;
			}
		}
		
		
		((BeanItemContainer<EtiquetaActiva>)tblAlertas.getContainerDataSource()).addAll(GiibptApplication.getAlertas());
		if(getWindow() != null){
			getWindow().executeJavaScript("setTimeout('javascript:vaadin.forceSync();', " + sleep+" );");	
		}
		
	}
	
	

}
